package org.uugu.counter.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import org.uugu.counter.R;
import org.uugu.counter.Support.CommonParam;
import org.uugu.counter.Support.DatabaseUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by Silence on 2014/3/28.
 */
public class ResultActivity extends Activity {

    // DB连接
    private DatabaseUtils dbUtils;

    // 横统计展示
    private GridView wgv;

    // 竖统计展示
    private GridView hgv;

    // GroupID
    private int groupId;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);

        wgv = (GridView)findViewById(R.id.wgridView);
        hgv = (GridView)findViewById(R.id.hgridView);

        dbUtils = new DatabaseUtils(this, CommonParam.DB_NAME, CommonParam.VERSION);
        Intent intent =getIntent();
        groupId = intent.getIntExtra("groupId", 0);

        Cursor cursor = dbUtils.getReadableDatabase().rawQuery("select width, sum(w_num) as wnum from data where group_id = ? group by width order by width desc", new String[]{String.valueOf(groupId)});
        List<Map<String, String>> searchResult = dbUtils.converCursorToList(cursor);
        SimpleAdapter sa = new SimpleAdapter(this, searchResult, R.layout.resultcell,
                new String[]{"width", "wnum"},
                new int[]{R.id.textView, R.id.textView2});
        wgv.setAdapter(sa);

        Cursor cursor2 = dbUtils.getReadableDatabase().rawQuery("select hight, sum(h_num) as hnum from data where group_id = ? group by hight order by hight desc", new String[]{String.valueOf(groupId)});
        List<Map<String, String>> searchResult2 = dbUtils.converCursorToList(cursor2);
        SimpleAdapter sa2 = new SimpleAdapter(this, searchResult2, R.layout.resultcell,
                new String[]{"hight", "hnum"},
                new int[]{R.id.textView, R.id.textView2});
        hgv.setAdapter(sa2);
    }
}